package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.PiPService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.vo.FmsMainVo;
import com.ruoyi.system.service.IFmsMainService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 财务主Controller
 * 
 * @author ruoyi
 * @date 2023-12-31
 */
@RestController
@RequestMapping("/system/main")
@RequiredArgsConstructor
@Slf4j
public class FmsMainController extends BaseController
{
    private final IFmsMainService fmsMainService;
    /**
     * 组合图表信息
     */
    @PreAuthorize("@ss.hasPermi('system:main:list')")
    @GetMapping("/getPipData/{type}")
    public AjaxResult getPipData(@PathVariable("type") Integer type) {
       PiPService piPService = fmsMainService.getPipData(type);
       return AjaxResult.success(piPService);
    }
    /**
     * 查询总金额根据类型，0收入，1支出
     */
    @PreAuthorize("@ss.hasPermi('system:main:query')")
    @GetMapping(value = "/getAllMoney/{type}")
    public AjaxResult getAllMoneyByType(@PathVariable("type") Integer type) {
        return AjaxResult.success("获取总金额成功",fmsMainService.asseFmsMoney(type));
    }
    /**
     * 查询财务明细列表
     */
    @PreAuthorize("@ss.hasPermi('system:main:list')")
    @GetMapping("/list")
    public TableDataInfo list(FmsMainVo fmsMain)
    {
        List<FmsMainVo> list = fmsMainService.selectFmsMainList(fmsMain);
        // 安全获取总数
        int totalCount = list.isEmpty() ? 0 :
                (list.get(0).getTotalCount() != null ? list.get(0).getTotalCount() : 0);

        return getDataTable(list, totalCount);
    }

    /**
     * 导出财务明细列表
     */
    @PreAuthorize("@ss.hasPermi('system:main:export')")
    @Log(title = "导出财务明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FmsMainVo fmsMain)
    {
        List<FmsMainVo> list = fmsMainService.selectFmsMainList(fmsMain);
        ExcelUtil<FmsMainVo> util = new ExcelUtil<FmsMainVo>(FmsMainVo.class);
        util.exportExcel(response, list, "财务明细数据");
    }

    /**
     * 获取财务明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:main:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(fmsMainService.selectFmsMainById(id));
    }

    /**
     * 新增财务明细
     */
    @PreAuthorize("@ss.hasPermi('system:main:add')")
    @Log(title = "新增财务明细", businessType = BusinessType.INSERT)
    @PostMapping
    @RepeatSubmit(interval = 3000,message = "数据处理中，请勿重复提交")
    public AjaxResult add(@Valid @RequestBody FmsMainVo fmsMain)
    {
        fmsMain.setCreateBy(getUsername());
        return toAjax(fmsMainService.insertFmsMain(fmsMain));
    }

    /**
     * 修改财财务明细
     */
    @PreAuthorize("@ss.hasPermi('system:main:edit')")
    @Log(title = "修改财务明细", businessType = BusinessType.UPDATE)
    @RepeatSubmit(interval = 3000,message = "数据处理中，请勿重复提交")
    @PutMapping
    public AjaxResult edit(@RequestBody FmsMainVo fmsMain)
    {
        fmsMain.setUpdateBy(getUsername());
        return toAjax(fmsMainService.updateFmsMain(fmsMain));
    }

    /**
     * 删除财务明细
     */
    @PreAuthorize("@ss.hasPermi('system:main:remove')")
    @Log(title = "删除财务明细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(fmsMainService.deleteFmsMainByIds(ids));
    }
}
